package cn.crex1.xiaomi.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import static cn.crex1.xiaomi.common.ProductEnum.CLIST;
import static cn.crex1.xiaomi.common.ProductEnum.PLIST;
import static cn.crex1.xiaomi.common.ProductEnum.CPLIST;
import static cn.crex1.xiaomi.common.ProductEnum.SEARCH;

import cn.crex1.xiaomi.common.ResponseCode;
import cn.crex1.xiaomi.common.ServerResponse;
import cn.crex1.xiaomi.pojo.Category;
import cn.crex1.xiaomi.pojo.Product;
import cn.crex1.xiaomi.service.ProductService;
import cn.crex1.xiaomi.service.impl.ProductServiceImpl;
import cn.crex1.xiaomi.util.JsonUtils;
import cn.crex1.xiaomi.util.PagesUtil;
import cn.crex1.xiaomi.util.StringUtil;
import cn.crex1.xiaomi.vo.ProductVO;

@WebServlet("/product/*")
public class ProductController extends HttpServlet {
	
	private ProductService productService = new ProductServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String[] split = req.getRequestURI().split("/");
		if (split[3].equals(CLIST.getDesc())) {
			requestCategoryListByTypeId(req, resp);
		}
		if (split[3].equals(PLIST.getDesc())) {
			requestProductList(req, resp);
		}if (split[3].equals(CPLIST.getDesc())) {
			requestCategoryProductByCategoryId(req, resp);
			
		}if (split[3].equals(SEARCH.getDesc())) {
			
		}else if (StringUtil.isInteger(split[3])) {
			System.out.println(split[3]);
			String product_number = split[3];
			if (product_number == null || product_number == "") {
				resp.sendRedirect(req.getContextPath() + "/pages/index.html");
			} else {
				List<Product> details = productService.selectOsproductByDetails(Long.parseLong(product_number));
				HttpSession session = req.getSession();
				session.setAttribute("details", details);
				resp.sendRedirect(req.getContextPath() + "/pages/xiangqing.jsp");
			}
		}
	
	}

	private void requestProductList(HttpServletRequest req, HttpServletResponse resp) {
		// 获取产品列表,
		//interval 返回产品数据列表的长度
		String interval = req.getParameter("interval");
		if (StringUtil.isExists(interval)) {
			ServerResponse<List<Product>> plist = productService.selectAllproduct(StringUtil.intValue(interval));
			if (plist != null) {
				JsonUtils.writeJsonStringByResponse(resp, JsonUtils.writeStringByObject(plist));
			}
		}
	}
	//获取分类列表
	private void requestCategoryListByTypeId(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("ctype");
		if (type != null) {
			ServerResponse<List<Category>> sc = productService.selectAllProductcategory(StringUtil.intValue(type));
			JsonUtils.writeJsonStringByResponse(response, JsonUtils.writeStringByObject(sc));
		}
	}
	//获取某个分类下的产品
	private void requestCategoryProductByCategoryId(HttpServletRequest request, HttpServletResponse response) {
		//分类ID
		String categoryId=request.getParameter("id");
		//分页 每页显示数据数
		String perPageDataCount=request.getParameter("pdcount");
		
		if (StringUtil.isExists(categoryId)) {
			ProductVO productVO=new ProductVO();
			
			productVO.setCategory_id(Integer.parseInt(categoryId));
			productVO.setPerPageDataCount(Integer.parseInt(perPageDataCount));
			
			ServerResponse<List<PagesUtil<ProductVO>>> cpList = productService.selectOProductByOCategoryId(productVO);
			
			JsonUtils.writeJsonStringByResponse(response, JsonUtils.writeStringByObject(cpList));
		}
	}
	/**
	 * 模糊查询产品列表
	 * @param request
	 * @param response
	 */
	private void requestProductListBySearchKeyWord(HttpServletRequest request, HttpServletResponse response) {
		
	}

}
